<?php
/**
 * @package PWE.modele
 * @author Gmax <gmaxfr78@gmail.com>
 */
class modele_translate extends modele {
    protected static $_table = 'translate';
    
    private $_id = 0;
    private $_modele = '';
    private $_modele_id = 0;
    private $_propriete = '';
    private $_content = '';
    private $_lang = '';
    
    private $_traductions = array();

    /**
     * construction du modele translate
     * @param int $id
     * @param array $options ['type'=>'propriete','lang'=>'','modele'=>'']
     */
    function __construct($id=null,$options=array('type'=>'propriete','lang'=>'','modele'=>'')){
        if($id!==null){
            if($options['type']==='propriete'){
                $this->_constructProperty($id);
            }elseif($options['type']==='object'){
                $this->_constructObject($id,$options['modele'],$options['lang']);
            }
        }
    }
    /**
     * construit le modele de la propriete demandée
     * @param int $id
     */
    private function _constructProperty($id){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.* ';
        $sqlQuery .= 'FROM '.self::getTable().' as t ';
        $sqlQuery .= 'WHERE t.id='.$id.' ';
        $sqlQuery .= 'LIMIT 1';
        
        $datas = $this->_getOneResult($sqlQuery);
        if($datas){
            $this->_id = $datas['id'];
            $this->_modele = $datas['modele'];
            $this->_modele_id = $datas['modele_id'];
            $this->_propriete = $datas['propriete'];
            $this->_lang = $datas['lang'];
            $this->_content = $datas['content'];
        }
    }
    /**
    * construit toute les propriété traduites pour la target indiqué en $id
    * @param int $id id du modele target
    * @param string $modele type du modele target
    * @param string $lang traduction cible
    */
    private function _constructObject($id,$modele='',$lang=''){
        $sqlQuery = 'SELECT ';
        $sqlQuery .= 't.* ';
        $sqlQuery .= 'FROM '.self::getTable().' as t ';
        $sqlQuery .= 'WHERE t.modele_id='.$id.' AND t.modele="'.$modele.'" ';
        if($lang!==''){
            $sqlQuery .= 'AND t.lang="'.$lang.'"';
        }
        
        $results = bdd_sql::doSql($sqlQuery,true);
        if($results['count']>0){
            $this->_modele = $modele;
            $this->_modele_id = $id;
            
            $datas = array();
            foreach ($results['results'] as $property){
                if(!isset($datas[$property['lang']])) $datas[$property['lang']] = array();
                $datas[$property['lang']][$property['propriete']] = $property['content'];
            }
            $this->_traductions = $datas;
        }
    }
    
    
    public function getId(){
        return $this->_id;
    }
    public function getModele(){
        return $this->_modele;
    }
    public function getModeleId(){
        return $this->_modele_id;
    }
    public function getPropriete(){
        return $this->_propriete;
    }
    public function getLangue(){
        return $this->_lang;
    }
    public function getContent($input=false){
        if($input)return $this->inputFormat($this->_content);
        else return $this->outputFormat($this->_content);
    }
    /**
     * renvoi l'objet cible de la traduction
     * @return null|modele
     */
    public function getTargetObject(){
        $target = null;
        if($this->getModeleId()>0) eval('$target = new modele_'.$this->getModele().'('.$this->getModeleId().');');
        return $target;
    }
    /**
     * renvoi le tableu de traduction par langue
     * @param string $lang si non précisé : renvoi toutes les langues disponibles
     * @param boolean $input
     * @return array
     */
    public function getTraduction($lang='',$input=false){
        $retour = array();
        if($lang!==''&&isset($this->_traductions[$lang])){
            $datas = $this->_traductions[$lang];
            $retour[$lang] = array();
            foreach($datas as $key=>$val){
                $retour[$lang][$key] = ($input)?$this->inputFormat($val):$this->outputFormat($val);
            }
        }else{
            foreach($this->_traductions as $l=>$datas){
                $retour[$l] = array();
                foreach($datas as $key=>$val){
                    $retour[$l][$key] = ($input)?$this->inputFormat($val):$this->outputFormat($val);
                }
            }
        }
        return $retour;
    }


    public function getDatas($input=false){
        $datas = array(
            'id'=>$this->getId(),
            'modele'=>$this->getModele(),
            'modele_id'=>$this->getModeleId(),
            'propriete'=>$this->getPropriete(),
            'content'=>$this->getContent($input),
            'lang'=>$this->getLangue()
        );
        return $datas;
    }
    public function setDatas($datas) {
        foreach($datas as $k=>$v){
            if(substr($k, 0, 1) !== '_') $k = '_'.$k;
            if(property_exists(get_class($this),$k)){
                $this->$k = $v;
            }
        }
        return true;
    }
    
    public function save(){
        $sqlQuery = 'INSERT INTO '.self::getTable().' ';
        $sqlQuery .= '(modele,modele_id,propriete,content,lang) ';
        $sqlQuery .= 'VALUES (';
        $sqlQuery .= '"'.$this->getModele().'",';
        $sqlQuery .= $this->getModeleId().',';
        $sqlQuery .= '"'.$this->getPropriete().'",';
        $sqlQuery .= '"'.$this->getContent(true).'",';
        $sqlQuery .= '"'.$this->getLangue().'"';
        $sqlQuery .= ')';
        
        bdd_sql::doSql($sqlQuery,false);
        
        return array('success'=>true,'datas'=>$this->getDatas());
    }
    public function update(){
        $sqlQuery = 'UPDATE '.self::getTable().' SET ';
        $sqlQuery .= 'content="'.$this->getContent(true).'" ';
        $sqlQuery .= 'WHERE id='.$this->getId().' LIMIT 1';
        bdd_sql::doSql($sqlQuery,false);

        return array('success'=>true,'datas'=>$this->getDatas());
    }
    
    public function delete() {
        $sqlDelete = 'DELETE FROM '.self::getTable().' WHERE id="'.$this->getId().'"';
        bdd_sql::doSql($sqlDelete,false);
        return array('success'=>true,'datas'=>array());
    }
    
    
    /* méthodes static */
    /**
     * 
     * @param int $modele_id
     * @param string $modele
     * @param string $propriete
     * @param string $lang
     * @return \modele_translate
     */
    public static function findObjectProperty($modele_id,$modele,$propriete,$lang){
        $sqlQuery = 'SELECT id FROM '.self::getTable().' WHERE propriete="'.$propriete.'" AND modele_id="'.$modele_id.'" AND modele="'.$modele.'" AND lang="'.$lang.'" LIMIT 1';
        $results = bdd_sql::doSql($sqlQuery,true);
        $retour = null;
        
        if($results['count']===1){
            $retour = new modele_translate($results['results'][0]['id']);
        }
        return $retour;
    }
    
    public static function findTranslatorByPermalien($lien){
        $sqlQuery = 'SELECT id FROM '.self::getTable().' WHERE propriete="lien" AND content="'.$lien.'" LIMIT 1';
        $results = bdd_sql::doSql($sqlQuery,true);
        $retour = null;
        
        if($results['count']===1){
            $retour = new modele_translate($results['results'][0]['id']);
        }
        return $retour;
    }
    
    /**
     * 
     * @return string
     */
    public static function getTable(){
        return PWE::getPrefixBdd().self::$_table;
    }
}

?>
